/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.util;

import java.io.Serializable;
import java.util.Map;
import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.el.ValueBinding;
import javax.faces.event.ActionEvent;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Thanh Luan
 */
public class ContextUtil {
    private static ServletContext applicationContext = null;
    //Thanh Luan Create 08-05-2012
    /*
     * param <f:attribute name="id" value="valuename" /> public void
     * action(ActionEvent event){String name = getActionAttribute(event,"id");}
     */

    public static String getActionAttribute(ActionEvent event, String attribute_name) {
        return (String) event.getComponent().getAttributes().get(attribute_name);
    }
    /*
     * param: <f:param name="id" value="valuename" />
     *
     */
    /*
     * String managedBeanName = FacesUtil.lookupManagedBeanName(this);
     */
    

    public static String getRequestParameter(String param_name) {
        return (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(param_name);
    }

    public static String lookupManagedBeanName(Object bean) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

        // Get requestmap.
        Map<String, Object> requestMap = externalContext.getRequestMap();

        // Lookup the current bean instance in the request scope.
        for (String key : requestMap.keySet()) {
            if (bean.equals(requestMap.get(key))) {
                // The key is the managed bean name.
                return key;
            }
        }

        // Bean is not in the request scope. Get the sessionmap then.
        Map<String, Object> sessionMap = externalContext.getSessionMap();

        // Lookup the current bean instance in the session scope.
        for (String key : sessionMap.keySet()) {
            if (bean.equals(sessionMap.get(key))) {
                // The key is the managed bean name.
                return key;
            }
        }

        // Bean is also not in the session scope. Get the applicationmap then.
        Map<String, Object> applicationMap = externalContext.getApplicationMap();

        // Lookup the current bean instance in the application scope.
        for (String key : applicationMap.keySet()) {
            if (bean.equals(applicationMap.get(key))) {
                // The key is the managed bean name.
                return key;
            }
        }

        // Bean is also not in the application scope.
        // Is the bean's instance actually a managed bean instance then? =)
        return null;
    }
    
    
    public static Object lookupBeanFromExternalServlet(String beanName,
			HttpServletRequest request, HttpServletResponse response) {

		LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder
				.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
		Lifecycle lifecycle = lFactory
				.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

		FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder
				.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);

		ServletContext servletContext = applicationContext;
		if(servletContext == null) {
			servletContext = request.getSession().getServletContext();
		}
		
		

		FacesContext facesContext = fcFactory.getFacesContext(servletContext,
				request, response, lifecycle);

		ApplicationFactory factory = (ApplicationFactory) FactoryFinder
				.getFactory(FactoryFinder.APPLICATION_FACTORY);
		Application application = factory.getApplication();
		Object bean = (Object) application.getVariableResolver()
				.resolveVariable(facesContext, beanName);
		return bean;
	}
}
